package com.cherrish.demo.thread;

import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author cherrish
 * @time 2019-02-27 14:12
 * @name TestThread3
 * @desc:
 */
public class TestThread3 {
    public static void main(String[] args) throws InterruptedException {
        FutureTask<Integer> future = new FutureTask<Integer>(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
// 返回一个值或受检查的异常
//throw new Exception();
                return new Random().nextInt(100);
            }
        });
        new Thread(future).start();
        ;
/**
  * 模拟其他业务逻辑
  */
        Thread.sleep(1000);
//Integer result = future.get(0, TimeUnit.SECONDS);
        Integer result = null;
        try {
            result = future.get();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        System.out.println("result========" + result);
    }
}
